package com.eet.rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * 生产者
 *
 * @author zheng.zhang
 */
public class Producer {

    public static void main(String[] args) {
        final ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("www.eet.ink");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");

        try (final Connection connection = factory.newConnection();
             final Channel channel = connection.createChannel()) {
            String exchangeName = "work.polling.exchange";
            String type = "fanout";
            channel.exchangeDeclare(exchangeName, type, true);

            String queueName = "work.queue.polling";
            channel.queueDeclare(queueName, true, false, false, null);

            channel.queueBind(queueName, exchangeName, "");

            for (int i = 0; i < 20; i++) {
                String message = "work测试轮询测试" + (i + 1);
                channel.basicPublish(exchangeName, "", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            }
            System.out.println("消息发送完成!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
